package jjn.round1;

import commons.ListNode;
import commons.TreeNode;

/**
 * @author Jjn
 * @since 2022/8/9 23:17
 */
public class LeetCode1367_LinkedListInBinaryTree {

    public boolean isSubPath(ListNode head, TreeNode root) {
        if (head == null) {
            return true;
        }
        if (root == null) {
            return false;
        }
        return isSame(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right);
    }

    private boolean isSame(ListNode head, TreeNode root) {
        if (head == null) {
            return true;
        }
        if (root == null) {
            return false;
        }
        if (head.val != root.val) {
            return false;
        }
        return isSame(head.next, root.left) || isSame(head.next, root.right);
    }
}
